package de.tuhh.simsearch.model.persistence.usermgmt.eao;

import static org.junit.Assert.assertEquals;

import java.math.BigInteger;

import javax.persistence.EntityManager;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


import de.tuhh.simsearch.dto.CategoryDTO;
//import de.tuhh.simsearch.controller.contentmgmt.interfaces.CategoryDTO;
import de.tuhh.simsearch.junit.Utilities;
import de.tuhh.simsearch.model.persistence.contentmgmt.eao.ContentMgmtEAO;
import de.tuhh.simsearch.model.persistence.contentmgmt.entities.CategoryEntity;
import de.tuhh.simsearch.model.persistence.contentmgmt.entities.FileEntity;

//import de.tuhh.simsearch.model.persistence.usermgmt.entities.UserEntity;

public class ContentMgmtTest {

	private EntityManager em;
	private ContentMgmtEAO eao;

	@BeforeClass
	public static void onlyOnceAtStart() {

		EntityManager entityman = Utilities.getContentEntityManager();
		ContentMgmtEAO entityao = new ContentMgmtEAO(entityman);

		entityman.getTransaction().begin();

		String fn = "myFavoriteFile";
		String path = "/test/file";
		entityman.persist(new FileEntity(fn, path));

		Integer cId = 1;
		String cName = "history";
		entityman.persist(new CategoryEntity(cId, cName));

		entityman.getTransaction().commit();

	}

	@AfterClass
	public static void onlyOnceAtEnd() {

	}

	@Before
	public void setUp() throws Exception {

		em = Utilities.getContentEntityManager();
		eao = new ContentMgmtEAO(em);
	}

	@Test
	public void testCategoryDTO() {

		String contentType = "Music";
		String categoryName = "Stupid";

		CategoryDTO cdto = new CategoryDTO(contentType, categoryName);
		assertEquals("contentType is " + contentType, contentType,
				cdto.getContentType());
		assertEquals("categoryName is " + categoryName, categoryName,
				cdto.getCategoryName());

		/*
		 * em.getTransaction().begin(); eao.addCategoryEntity(new
		 * CategoryEntity(Constants.descContentIdMap.get(cdto.getContentType()),
		 * categoryName)); em.getTransaction().commit();
		 */
	}

	
	
	@Test
  public void testaddFileEntity() {

    em.getTransaction().begin();

    String fn = "myFavoriteFile";
    String path = "/test/file";
    FileEntity myFileEntity = new FileEntity(fn, path);
    assertEquals("Did add file work?", true, eao.addFileEntity(myFileEntity));

		em.getTransaction().commit();
	}

	@Test
	public void testaddCategoryEntity() {

    em.getTransaction().begin();

    Integer cId = 1;
    String cName = "history";
    CategoryEntity myCategoryEntity = new CategoryEntity(cId, cName);

    assertEquals("Did add file work?", true, eao.addCategoryEntity(myCategoryEntity));

    em.getTransaction().commit();
  }

	@Test
	public void testaddFileEntityToCategory(){
	  Integer cId = 1;
    String cName = "history";
    String fn = "myFavoriteFile";
    String path = "/test/file";
    FileEntity myFileEntity = new FileEntity(fn, path);
  //  BigInteger id=myFileEntity.getId();
    em.persist(new FileEntity(fn, path)); // wieder herausnehmen
    assertEquals("Did add file to category work?", true, eao.addFileEntityToCategory(myFileEntity,cName,cId));

    em.getTransaction().commit();
	  
	}
	
	@Test
  public void testremoveCategoryEntity(){
	  em.getTransaction().begin();
	  Integer cId = 1;
    String cName = "history";
    String fn = "myFavoriteFile";
    String path = "/test/file";
    FileEntity myFileEntity = new FileEntity(fn, path);
    BigInteger id=myFileEntity.getId();
    em.persist(myFileEntity); // wieder herausnehmen
    // die 1 ist noch ein Platzhalte
    assertEquals("Did remove category work?", true, eao.removeCategoryEntity(cName,cId,myFileEntity));

    em.getTransaction().commit();
	}
	
}
